// 数据库操作类
import config from "@/configs/config.js"
const sqliteUtil = {
	dbName: "",
	dbPath: "",
	iniDb(dbName, dbPath) {
		this.dbName = dbName
		this.dbPath = dbPath
	},
	isOpen() { //数据库是否打开
		console.log("isOpen", this.dbName, this.dbPath);
		var open = plus.sqlite.isOpenDatabase({
			name: this.dbName, // 数据库名称
			path: this.dbPath // 数据库地址  
		})
		return open
	},
	openSqlite() { //打开数据库
		return new Promise((resolve, reject) => {
			plus.sqlite.openDatabase({ // 打开数据库
				name: this.dbName,
				path: this.dbPath,
				success(e) {
					resolve(e); // 成功回调
				},
				fail(e) {
					reject(e); // 失败回调
				}
			})
		})
	},

	closeSqlite(dbName) { // 关闭数据库
		return new Promise((resolve, reject) => {
			plus.sqlite.closeDatabase({
				name: this.dbName,
				success(e) {
					resolve(e);
				},
				fail(e) {
					reject(e);
				}
			})
		})
	},
	executeSQL(sql) { //执行sql
		return new Promise((resolve, reject) => {
			// 表格查询数据  执行查询的SQL语句
			plus.sqlite.selectSql({
				name: this.dbName,
				sql: sql,
				success(e) {
					// console.log("executeSQL-success", e);
					resolve(e);
				},
				fail(e) {
					console.log("executeSQL-fail", e);
					reject(e);
				}
			})
		})
	}
}
export default sqliteUtil